﻿/*
Copyright © 2010 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Faz.Dependencies.Controls
{
    /// <summary>
    /// A visual component used to display the header of the reference part
    /// of a <see cref="DependencyView"/>.
    /// </summary>
    /// <seealso cref="DependencyView"/>
    internal class DependencyViewReference : ListView
    {
        /// <summary>
        /// Initializes static members of the DependencyViewReference class.
        /// </summary>
        static DependencyViewReference()
        {
            DefaultStyleKeyProperty.OverrideMetadata(
                typeof(DependencyViewReference),
                new FrameworkPropertyMetadata(typeof(DependencyViewReference)));
        }

        /// <summary>
        /// Initializes a new instance of the DependencyViewReference class.
        /// </summary>
        public DependencyViewReference()
        {
            Trace.Default.TraceVerbose("DependencyViewReference.ctor()");
        }

        /// <summary>
        /// Gets the encapsulating <see cref="ListView"/> instance.
        /// </summary>
        protected ListView ListView
        {
            get { return this.TemplatedParent as ListView; }
        }

        /// <summary>
        /// Gets the associated <see cref="DependencyView"/> instance.
        /// </summary>
        protected DependencyView DependencyView
        {
            get { return this.ListView.View as DependencyView; }
        }

        /// <summary>
        /// Called when the component is initialized.
        /// </summary>
        /// <param name="e">The <see cref="RoutedEventArgs"/> that contains the event data.</param>
        /// <remarks>
        /// This method ensures that the items of the component are
        /// the references of the associated <see cref="DependencyView"/>.
        /// </remarks>
        protected override void OnInitialized(EventArgs e)
        {
            Trace.Default.TraceVerbose("DependencyViewReference.OnInitialized()");
            this.SetBinding(ItemsSourceProperty, new Binding("References") { Source = this.DependencyView });

            this.SetBinding(ItemContainerStyleProperty, new Binding("ReferenceContainerStyle") { Source = this.DependencyView });
            this.SetBinding(ItemContainerStyleSelectorProperty, new Binding("ReferenceContainerStyleSelector") { Source = this.DependencyView });
            this.SetBinding(ItemStringFormatProperty, new Binding("ReferenceStringFormat") { Source = this.DependencyView });
            this.SetBinding(ItemTemplateProperty, new Binding("ReferenceTemplate") { Source = this.DependencyView });
            this.SetBinding(ItemTemplateSelectorProperty, new Binding("ReferenceTemplateSelector") { Source = this.DependencyView });

            base.OnInitialized(e);
        }
    }
}
